<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\PageCategory
 *
 * @property int $catid 分类ID
 * @property string|null $name 分类名称
 * @property int $displayorder 排序
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Page[] $pages
 * @property-read int|null $pages_count
 * @method static Builder|PageCategory newModelQuery()
 * @method static Builder|PageCategory newQuery()
 * @method static Builder|PageCategory query()
 * @method static Builder|PageCategory whereCatid($value)
 * @method static Builder|PageCategory whereDisplayorder($value)
 * @method static Builder|PageCategory whereName($value)
 * @mixin \Eloquent
 */
class PageCategory extends Model
{
    protected $table = 'page_category';
    protected $primaryKey = 'catid';
    protected $fillable = ['name', 'displayorder'];
    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::addGlobalScope('sort', function (Builder $builder) {
            return $builder->orderBy('displayorder')->orderBy('catid');
        });
        static::deleting(function (PageCategory $category) {
            $category->pages()->delete();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function pages()
    {
        return $this->hasMany(Page::class, 'catid', 'catid');
    }
}
